package cn.edu.zsc.rbac.service.array;

import cn.edu.zsc.rbac.domain.dto.UserDto;
import cn.edu.zsc.rbac.domain.entity.Dept;
import cn.edu.zsc.rbac.domain.entity.Role;
import cn.edu.zsc.rbac.domain.entity.User;
import cn.edu.zsc.rbac.domain.vo.Result;
import cn.edu.zsc.rbac.domain.vo.UserVo;
import cn.edu.zsc.rbac.service.IDeptService;
import cn.edu.zsc.rbac.service.IRoleService;
import cn.edu.zsc.rbac.service.IUserService;

import java.util.Arrays;

public class UserService extends BaseService<User,UserDto>implements IUserService {


    private final IDeptService deptService;
    private final IRoleService roleService;

    private static IUserService singleton;

    private UserService(){
        deptService=DeptService.getInstance();
        roleService=RoleService.getInstance();
        data=new User[DATA_SIZE+1];
    }

    public static IUserService getInstance(){
        if(singleton==null){
            singleton=new UserService();
        }
        return singleton;
    }




    public  UserVo convertToVo(User user){
        String deptName = "";
    Dept dept=deptService.findById(user.deptId);
    if(dept !=null){
        deptName=dept.name;
    }
    String roleName="";
    Role role=roleService.findById(user.roleId);
    if(role !=null){
        roleName=role.name;
    }
    return new UserVo(user.getId(),user.username,user.name,user.mobile,user.email,
            deptName,roleName,user.deptId,user.roleId,user.enabled);
    }



    public  User createEntity(UserDto dto, int id) {

        return new User(id,dto.deptId,dto.emall,dto.mobile,dto.name,dto.roleId,dto.username);
    }

    public  void updateEntity(UserDto dto, User user) {
        user.username = dto.username;
        user.name = dto.name;
        user.mobile = dto.mobile;
        user.email = (String) dto.email;
        user.deptId = dto.deptId;
        user.roleId = dto.roleId;
    }

    @Override
    public  boolean deptInUse(int deptId){
        boolean inUse=false;
        for(User user:data){
            if(user != null &&user.deptId==deptId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }

    @Override
    public  boolean roleInUse(int roleId){
        boolean inUse=false;
        for(User user:data){
            if(user != null &&user.roleId==roleId){
                inUse=true;
                break;
            }
        }
        return inUse;
    }


    @Override

    public  Result list(){
        UserVo[] temp=new UserVo[DATA_SIZE];
        int length=0;
        for (int i = 0; i < idCounter; i++) {
            if(data[i]!=null){
                temp[length++]=convertToVo(data[i]);
            }
        }
        return Result.success("查询成功", Arrays.copyOf(temp,length));
    }

    @Override
    public  Result create(UserDto dto) {
        if (idCounter > DATA_SIZE) {
            return Result.failure("创建失败，超出了存储容量");
        }
        if (!deptService.existsById(dto.deptId)) {
            return Result.failure("创建失败，不存在指定的部门");
        }
        if (!roleService.existsById(dto.roleId)) {
            return Result.failure("创建失败，不存在指定的角色");
        }
        User user = createEntity(dto, idCounter++);
        data[user.getId()] = user;
        return Result.success("创建成功", null);
    }
    @Override
    public  Result update(UserDto dto,int id){
        if(!existsById(id)) {
            return Result.failure("更新失败，不存在该数据");
        }
            if (!deptService.existsById(dto.deptId)) {
                return Result.failure("创建失败，不存在指定的部门");
            }
            if (!roleService.existsById(dto.roleId)) {
                return Result.failure("创建失败，不存在指定的角色");
            }

        User user=data[id];
        updateEntity(dto,user);
        return Result.success("更新成功",null);
    }

    @Override
    public  Result toggle(int id){
        if(!existsById(id)){
            return Result.failure("切换状态失败，不存在该数据");
        }
        data[id].enabled= !data[id].enabled;
        return Result.success(data[id].enabled?"启动成功":"停用成功",null);
    }
}
